/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.couponinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.couponinfo.entity.CouponInfo;
import com.thinkgem.jeesite.modules.couponinfo.service.CouponInfoService;

/**
 * 生成加息券Controller
 * @author thinkgem
 * @version 2017-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/couponinfo/couponInfo")
public class CouponInfoController extends BaseController {

	@Autowired
	private CouponInfoService couponInfoService;
	
	@ModelAttribute
	public CouponInfo get(@RequestParam(required=false) String id) {
		CouponInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = couponInfoService.get(id);
		}
		if (entity == null){
			entity = new CouponInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("couponinfo:couponInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(CouponInfo couponInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CouponInfo> page = couponInfoService.findPage(new Page<CouponInfo>(request, response), couponInfo); 
		model.addAttribute("page", page);
		return "modules/couponinfo/couponInfoList";
	}

	@RequiresPermissions("couponinfo:couponInfo:view")
	@RequestMapping(value = "form")
	public String form(CouponInfo couponInfo, Model model) {
		model.addAttribute("couponInfo", couponInfo);
		return "modules/couponinfo/couponInfoForm";
	}

	@RequiresPermissions("couponinfo:couponInfo:edit")
	@RequestMapping(value = "save")
	public String save(CouponInfo couponInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, couponInfo)){
			return form(couponInfo, model);
		}
		if (couponInfo.getIsNewRecord()) {
			couponInfo.setCreateTime(new java.util.Date());
		}
		couponInfoService.save(couponInfo);
		addMessage(redirectAttributes, "保存加息券成功");
		return "redirect:"+Global.getAdminPath()+"/couponinfo/couponInfo/?orderBy=couponSort asc,createTime desc";
	}
	
	@RequiresPermissions("couponinfo:couponInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(CouponInfo couponInfo, RedirectAttributes redirectAttributes) {
		couponInfoService.delete(couponInfo);
		addMessage(redirectAttributes, "删除加息券成功");
		return "redirect:"+Global.getAdminPath()+"/couponinfo/couponInfo/?orderBy=couponSort asc,createTime desc";
	}

}